package pers.qianyu.filter;


import cn.hutool.core.util.*;
import org.slf4j.*;
import org.springframework.core.annotation.*;
import org.springframework.stereotype.*;

import javax.servlet.*;
import javax.servlet.annotation.*;
import java.io.*;
import java.util.*;

/**
 * TraceId 过滤器
 *
 * @author mizzle rain
 * @date 2020-10-14 10:28
 */
@Order(1)
@WebFilter(urlPatterns = "/")
@Component
public class TraceIdFilter implements Filter {
    /**
     * traceId 常量
     */
    public static final String TRACE_ID = "traceId";

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        String traceId = req.getParameter(TRACE_ID);
        if (StrUtil.isBlank(traceId)) {
            traceId = UUID.randomUUID().toString();
        }
        MDC.put(TRACE_ID, traceId);
        filterChain.doFilter(req, res);
    }
}
